/*---------------------------------------------------------------------------------

	by GugloPWN

---------------------------------------------------------------------------------*/

#include "map.h"




void map::readmap(char path[64])
{
	int i,j;
	char filename[64],tempname[64];
	
	
	for(i=0;i<16;i++)
	{
		for(j=0;j<16;j++)
		{
			
			
			sprintf(filename,"%s%02d%02d",path,i,j);
			strcpy(tempname,filename);
			strcat(tempname,".vmap");
			FILE* file = fopen(tempname,"rb");
	
			if(file == NULL) 
			{
				printf("\n%s block not found",filename);
				blks[i][j] = new block();
			}
			else
			{
				fclose(file);
				blks[i][j] = new block(filename);
			}
		}
	}
	
	//Assemble the block texture file name
	strcpy(filename,path);
	strcat(filename,"block_tex.bmp");
	
	printf("\nLoading Block Texture");
	glGenTextures(1, TexBlk);
	LoadModelTexture(filename,TexBlk,TexBlkPal,&TexBlkSize) ;
	
	//Assemble the Deco Obj texture file name
	strcpy(filename,path);
	strcat(filename,"deco_tex.bmp");
	
	printf("\nLoading Deco Texture");
	glGenTextures(1, TexDeco);
	LoadModelTexture(filename,TexDeco,TexDecoPal,&TexDecoSize) ;
	
	//Assemble the Deco Obj 3D filename
	for(i=0;i<16;i++)
	{
		sprintf(filename,"%s_deco%02d.md2",path,i,j);
		
		printf("Loading %s\n",filename);
		LoadMD2Model(filename,256,1,Deco_Model[i],Deco_ModelEnable[i]);
	}
	printf("Done.\n");
	
}

void map::getdefaultmap()
{
	int i,j;
	
	for(i=0;i<5;i++)
	{
		for(j=0;j<5;j++)
		{
			blks[i][j] = new block();
			//printf("\nloaded block %d %d",i,j);
		}
	}
	
	for(i=0;i<5;i++)
	{
		blks[i][0]-> getnullblock();
		blks[0][i]-> getnullblock();
		blks[i][4]-> getnullblock();
		blks[4][i]-> getnullblock();
	}

}

void map::loadmapmodels()
{
	int i,j;
	
	for(i=0;i<16;i++)
	{
		for(j=0;j<16;j++)
		{
		}
	}
}

void map::drawmap(int mx, int my)
{
	//glLoadIdentity();
	
	glBindTexture (GL_TEXTURE_2D, TexBlk[0]);
	glColorTable(GL_RGB256, TexBlkPal[0]);
	
	//the block the player is in
	blks[mx][my]->drawblock();
	
	glBindTexture (GL_TEXTURE_2D, TexDeco[0]);
	glColorTable(GL_RGB256, TexDecoPal[0]);
	
	drawmapblockdeco(mx,my);
	
	
	
	/*
	//the blocks at right angles to the players
	glTranslatef(2,0,0);
	blks[mx+1][my]->drawblock();
	glTranslatef(-4,0,0);
	blks[mx-1][my]->drawblock();
	glTranslatef(2,2,0);
	blks[mx][my+1]->drawblock();
	glTranslatef(0,-4,0);
	blks[mx][my-1]->drawblock();
	
	
	//the blocks on the diagnals
	glTranslatef(2,4,0);
	blks[mx+1][my+1]->drawblock();
	glTranslatef(-4,0,0);
	blks[mx-1][my+1]->drawblock();
	glTranslatef(0,-4,0);
	blks[mx-1][my-1]->drawblock();
	glTranslatef(4,0,0);
	blks[mx+1][my-1]->drawblock();
	*/

}

void map::drawmapcolmap(int mx, int my)
{
	//glLoadIdentity();
	
	//the block the player is in
	blks[mx][my]->drawblockcolmap();
	/*
	//the blocks at right angles to the players
	glTranslatef(2,0,0);
	blks[mx+1][my]->drawblockcolmap();
	glTranslatef(-4,0,0);
	blks[mx-1][my]->drawblockcolmap();
	glTranslatef(2,2,0);
	blks[mx][my+1]->drawblockcolmap();
	glTranslatef(0,-4,0);
	blks[mx][my-1]->drawblockcolmap();
	*/
	/*
	//the blocks on the diagnals
	glTranslatef(2,4,0);
	blks[mx+1][my+1]->drawblockcolmap();
	glTranslatef(-4,0,0);
	blks[mx-1][my+1]->drawblockcolmap();
	glTranslatef(0,-4,0);
	blks[mx-1][my-1]->drawblockcolmap();
	glTranslatef(4,0,0);
	blks[mx+1][my-1]->drawblockcolmap();
	*/
	
	/*
	//the blocks at right angles to the players
	glTranslatef(2,0,0);
	blks[mx+1][my]->drawblockcolmap();
	glTranslatef(-4,0,0);
	blks[mx-1][my]->drawblockcolmap();
	glTranslatef(2,2,0);
	blks[mx][my+1]->drawblockcolmap();
	glTranslatef(0,-4,0);
	blks[mx][my-1]->drawblockcolmap();
	
	//the blocks on the diagnals
	glTranslatef(2,4,0);
	blks[mx+1][my+1]->drawblockcolmap();
	glTranslatef(-4,0,0);
	blks[mx-1][my+1]->drawblockcolmap();
	glTranslatef(0,-4,0);
	blks[mx-1][my-1]->drawblockcolmap();
	glTranslatef(4,0,0);
	blks[mx+1][my-1]->drawblockcolmap();
	*/

}

void map::drawmapblockdeco(int mx, int my)
{
	deco cur_deco;
	int i;
	for(i=0;i<16;i++)
	{
		cur_deco = blks[mx][my]->getdeco(i);
		
		if((Deco_ModelEnable[cur_deco.type] == true) && (cur_deco.x <= 20))
		{
			glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE);
			glPushMatrix();
	
			glTranslatef(((float)cur_deco.x)/10.0,((float)cur_deco.y)/10.0,((float)cur_deco.z)/40.0);
			glRotatef(90.0*cur_deco.rot,0.0f,0.0f,1.0f);
			
	
			//glBindTexture (GL_TEXTURE_2D, Texture[0]);
			//glColorTable(GL_RGB256, TexturePal[0]);		
			RenderMD2Model(0,Deco_Model[cur_deco.type],Deco_ModelEnable[cur_deco.type]);
	
			glPopMatrix(1);
		}
	}
	
}

/*
void map::updatemap()
{
	p1->updateplayer();
	p1->setplayercam();
	//glLoadIdentity();
	//gluLookAt(  0, 0, 1,	//camera possition 
	//			2, 2, 0,				//look at
	//			0.0, 0.0, 1.0);	
				
}*/






